package task;

import java.util.*;

public class 数组去重 {
    //7、int[] arr1 = {1, 2, 3, 4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3}; 数组去重
    //1.用list集合实现
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
        List<Integer> list=new ArrayList<Integer>();
        for (int i = 0; i < arr1.length; i++) {
            if (!list.contains(arr1[i])){
                list.add(arr1[i]);
            }
        }
        System.out.println("list集合去重后的数组为："+list);
        //2.用hashSet,TreeSet实现
        Integer[] nums={5,5,6,6,6,8,8,7,11,12,12};
        //HashSet=new HashSet(Arrays.asList(nums));
        TreeSet<Integer> hset=new TreeSet<Integer>(Arrays.asList(nums));
        Iterator i=hset.iterator();
        while ((i.hasNext())) {
            System.out.println(i.next());
        }
        //3.用list和set实现
        int[] arr2={5,6,6,6,8,8,7};
        List<Integer> numList=new ArrayList<Integer>();
        for (int num: arr2) {
            numList.add(num);
            Set<Integer> numSet = new HashSet<Integer>(numList);
            //如果上面的（）没有numlist则在下一行加一个numSet.addAll(numList);
            System.out.println(numSet);
        }
    }
}
